package com.free.semantic.common;

import com.free.semantic.core.IClone;
import com.free.semantic.core.script.impl.ScriptEngineImpl;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

public class RangeObj implements IClone<RangeObj>, Comparable<RangeObj> {

    private Range range;

    private String objectName;

    @Override
    public String toString() {
        return String.format("%s%s", range, objectName);
    }

    public static RangeObj parse(String rangeObjStr) {
        return ScriptEngineImpl.convertToRangeObj(rangeObjStr);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        RangeObj that = (RangeObj) o;
        return range.equals(that.range) && objectName.equals(that.objectName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(range, objectName);
    }

    /**
     * this 是提供的，参数是要求的
     *
     * @param another
     * @return
     */
    public boolean match(RangeObj another) {
        if (!Objects.equals(objectName, another.objectName)) {
            return false;
        }
        // 提供的不是1，但是要求是1
        if (range != Range.ONE && another.getRange() == Range.ONE) {
            return false;
        }
        return true;
    }

    public static RangeObj one(String objectName) {
        RangeObj objectInfo = new RangeObj();
        objectInfo.range = Range.ONE;
        objectInfo.objectName = objectName;
        return objectInfo;
    }

    public static RangeObj lessOne(String objectName) {
        RangeObj objectInfo = new RangeObj();
        objectInfo.range = Range.LESS_ONE;
        objectInfo.objectName = objectName;
        return objectInfo;
    }

    public static RangeObj more(String objectName) {
        RangeObj objectInfo = new RangeObj();
        objectInfo.range = Range.MULTIPLE;
        objectInfo.objectName = objectName;
        return objectInfo;
    }

    public RangeObj withOtherName(String name) {
        if (StringUtils.isBlank(name)) {
            throw new IllegalArgumentException();
        }
        RangeObj rangeObj = new RangeObj();
        rangeObj.setRange(this.range);
        rangeObj.setObjectName(name);
        return rangeObj;
    }

    public RangeObj withOtherRange(Range range) {
        RangeObj rangeObj = new RangeObj();
        rangeObj.setRange(range);
        rangeObj.setObjectName(this.objectName);
        return rangeObj;
    }

    @Override
    public RangeObj clone() {
        RangeObj res = new RangeObj();
        res.setObjectName(this.objectName);
        res.setRange(this.range);
        return res;
    }

    @Override
    public int compareTo(RangeObj o) {
        return o.objectName.compareTo(this.objectName);
    }

    public Range getRange() {
        return range;
    }

    public void setRange(Range range) {
        this.range = range;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

}
